<?php
/**
 * CreateTime: 2023/3/3 10:27
 */

namespace App\Http\Controllers\MeLawsuit;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Http\Requests\ListBasicRequest;
use App\Http\Requests\MeLawsuit\MeAddLawsuitRecordRequest;
use App\Models\MeLawsuit\MeLawsuitRecordModel;

class MeLawsuitRecordController
{
    public function addLawsuitRecord(MeAddLawsuitRecordRequest $request){
        $data = $request->all();
        // 处理文件
        if(isset($data['files'])){
            $data['file_ids'] = implode(",",array_column($data['files'],"file_id"));
            unset($data['files']);
        }
        // 设置默认值
        $data['owner_id'] = TokenFuc::getCurrentStaffInfo()['staff_id'];
        $model = new MeLawsuitRecordModel();
        $res = $model->add($data);
        if($res){
            return ResponseFunc::returnJson(
                'ok',
                [
                    'lawsuit_record_id' => $res
                ],
                '添加诉讼记录成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
        return ResponseFunc::returnJson(
            'error',
            null,
            '添加诉讼记录失败',
            ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
        );
    }

    public function editLawsuitRecord(int $lawsuitRecordId){
        $model = new MeLawsuitRecordModel();
        $data = request()->all();
        if(isset($data['files'])){
            $data['file_ids'] = implode(",",array_column($data['files'],"file_id"));
            unset($data['files']);
        }
        $data['lawsuit_record_id'] = $lawsuitRecordId;
        try {
            $res = $model->updateData($data);
            return ResponseFunc::returnJson(
                'ok',
                $res ?? [],
                '修改诉讼记录成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '修改诉讼记录失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
    }

    public function getLawsuitRecordById(int $lawsuitRecordId){
        $model = new MeLawsuitRecordModel();
        $res = $model->getById($lawsuitRecordId,[
            'lawsuit_record_id',
            'lawsuit_record_content',
            'file_ids',
            'owner_id',
            'owner_id as owner_str',
            'status',
        ]);
        return ResponseFunc::returnJson(
            'ok',
            $res ?? [],
            '获取诉讼记录成功',
            ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
        );
    }

    public function getLawsuitRecordByList(ListBasicRequest $request){
        try {
            $pFilters = $request->get('filters');
            $pageSize = $request->get('page_size');
            $filters = $pFilters ? json_decode($pFilters,true) : [];
            $model = new MeLawsuitRecordModel();
            $query = $model->select([
                'lawsuit_record_id',
                'lawsuit_record_content',
                'file_ids',
                'owner_id',
                'owner_id as owner_str',
                'status',
            ])->where('status',BaseConsts::STATUS_NORMAL);
            if($filters){
                if(isset($filters['lawsuit_record_content']) && $filters['lawsuit_record_content']){
                    $query = $query->where('lawsuit_record_content','LIKE',"%".$filters['lawsuit_record_content']."%");
                }
            }
            $data = $model->getListNoTime($query,$pageSize)->toArray() ?? [];
            return ResponseFunc::returnJson(
                'ok',
                $data,
                '获取诉讼记录列表成功',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '获取诉讼记录列表失败',
                ResponseStatusConsts::CREATED_201_RESPONSE_STATUS
            );
        }
    }

    public function deleteLawsuitRecordById(){

    }
}